package com.lwx.bus.controller;


import com.lwx.bus.domain.Provider;
import com.lwx.bus.service.ProviderService;
import com.lwx.sys.utils.DataGridView;
import com.lwx.sys.utils.ResultObj;
import com.lwx.sys.vo.ProviderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cn.lwx
 * @since 2020-04-21
 */
@RestController
@RequestMapping("provider")
public class ProviderController {
    @Autowired
    private ProviderService providerService;

    @GetMapping("/loadAllProvider")
    public DataGridView loadAllProvider(ProviderVo providerVo){
        return providerService.loadAllProvider(providerVo);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteProvider")
    public ResultObj deleteProvider(Integer id){
        try {
            providerService.deleteProvider(id);
            return ResultObj.DELETE_SUCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/batchDeleteProvider")
    public ResultObj batchDeleteProvider(Integer[] ids){
        try {
            providerService.batchDeleteProvider(ids);
            return ResultObj.DELETE_SUCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 添加用户
     * @param provider
     * @return
     */
    @PostMapping("/addProvider")
    public ResultObj addProvider(Provider provider){
        try {
            if (provider.getProvidername() == null){
                return ResultObj.ADD_ERROR;
            }
            providerService.addProvider(provider);
            return ResultObj.ADD_SUCESS;
        }catch (Exception e){

            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 更新商品
     * @param provider
     * @return
     */
    @PostMapping("/updateProvider")
    public ResultObj updateProvider(Provider provider){
        try {
            providerService.updateProvider(provider);
            return ResultObj.ADD_SUCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/loadProviderById")
    public DataGridView loadProviderById(Integer id){
        return new DataGridView(providerService.getById(id));
    }

    /**
     * 加载所有的可用数据
     * @return
     */
    @GetMapping("/loadAllAvailableProvider")
    public DataGridView loadAllAvailableProvider(){
        return providerService.queryAllAvailableProvider();
    }
}

